﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TTQT.TTQT_Code;
public partial class InPhieuNhapKho : Page
{
    double tongTien = 0;
    private void bindGridDetail(string maHoaDon)
    {
        lblTongTienBangSo.Text = "";
        lblTongTienBangChu.Text = "";
        lblDetailGrid.Text = "";
        tongTien = 0;
        ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        ds.SelectCommand = "HoaDon_Detail_getByMaHoaDon_print";
        ds.SelectParameters.Clear();
        ds.SelectParameters.Add(new Parameter("mahoadon"));
        ds.SelectParameters["mahoadon"].DefaultValue = lblMaHoaDon.Text;
        DataTable t = ((DataView)(ds.Select(new DataSourceSelectArguments()))).ToTable();
        int stt = 1;
        foreach (DataRow r in t.Rows)
        {
            lblDetailGrid.Text = lblDetailGrid.Text + @"<tr>
            <td class=tdDep>" + stt.ToString() + "</td>" +
                                 "<td class=tdDep>" + r["TenAnChi"].ToString() + "</td>" +
                                 "<td class=tdDep>" + r["LoaiAnChi"].ToString() + "</td>" +
                                 "<td class=tdDep>" + r["DonViTinh"].ToString() + "</td>" +
                                 "<td class=tdDep>" + TTQTCommon.ToNumberFormat(r["ChiSoDau"].ToString()) + "</td>" +
                                 "<td class=tdDep>" + TTQTCommon.ToNumberFormat(r["ChiSoCuoi"].ToString()) + "</td>" +
                                 "<td class=tdDep>" + TTQTCommon.ToNumberFormat(r["SoLuong"].ToString()) + "</td>" +
                                 "<td class=tdDep>" + r["Seri"].ToString() + "</td>" +
                                 "<td class=tdDep>" + TTQTCommon.ToNumberFormat(r["DonGia"].ToString()) + "</td>" +
                                 "<td class=tdDep align=right>" + TTQTCommon.ToNumberFormat(r["ThanhTien"].ToString()) + "</td></tr>";
            tongTien = tongTien + double.Parse(r["ThanhTien"].ToString());
            stt++;
        }
        lblTongTienBangChu.Text = KK.KKCommon.DocSoTien(tongTien.ToString(), "Đồng");
        lblTongTienBangSo.Text = TTQTCommon.ToNumberFormat(tongTien.ToString());
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string khoXuat = "", khoNhan = "";
            khoXuat = Request.QueryString["khoXuat"].ToString();
            khoNhan = Request.QueryString["khoNhan"].ToString();
            //Lấy Tên chi nhánh
            try
            {
                dsTenChiNhanh.SelectCommandType = SqlDataSourceCommandType.Text;
                dsTenChiNhanh.SelectCommand = "Select b.BranchName from BrnStatus b where b.BranchCode ='" + khoNhan + "'";
                lblChiNhanh.Text =
                    ((DataView)dsTenChiNhanh.Select(new DataSourceSelectArguments())).ToTable().Rows[0][0].ToString();

               
            }
            catch (Exception tenChiNhanh)
            {
            }
            try
            {
                 dsTenChiNhanh.SelectCommand = "Select b.TenKho from MaKho b where b.MaKho ='" + khoXuat + "'";
                lblDonVi.Text =
                    ((DataView)dsTenChiNhanh.Select(new DataSourceSelectArguments())).ToTable().Rows[0][0].ToString();

            }catch{}
            //Lấy thông tin hóa đơn
            lblMaHoaDon.Text = Request.QueryString["MaHoaDon"].ToString();
            bindGridDetail(lblMaHoaDon.Text);
            ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            ds.SelectCommand = "HoaDon_Master_getByMaHoaDon_print";
            ds.SelectParameters.Clear();
            ds.SelectParameters.Add(new Parameter("mahoadon"));
            ds.SelectParameters["mahoadon"].DefaultValue = lblMaHoaDon.Text;
            DataTable rs = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();
            if (rs != null)
            {
                try
                {
                    lblNgay.Text = rs.Rows[0]["NgayN"].ToString();
                    lblThang.Text = rs.Rows[0]["ThangN"].ToString();
                    lblNam.Text = rs.Rows[0]["NamN"].ToString();
                    //lblNguoiNhan_CMT.Text = rs.Rows[0]["CMT"].ToString();
                }
                catch (Exception exx) { }
                try
                {
                    lblNguoiNhan_FullName.Text = rs.Rows[0]["user_fullName"].ToString();
                    //lblDonVi.Text = rs.Rows[0]["DonVi"].ToString();
                    //lblNguoiNhan_CMT.Text = rs.Rows[0]["CMT"].ToString();
                }
                catch { }

            }

            bindGridDetail(lblMaHoaDon.Text);
        }

        //btnPrint.Attributes.Add("OnClick", "printPage(" + pnPrint.ClientID + "); return false;");
        string dinhDang = Request.QueryString["DinhDang"];
        if (null != dinhDang)
        {
            if (dinhDang.Equals("doc"))
            {
                Response.ContentType = "application/msword";
            }
            else
            {
                base.OnPreRender(e);
            }
        }
        else
        {
            base.OnPreRender(e);
        }
    }

    protected void rptHoaDon_Detail_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            //if (null != c)
            //{
            //    if (danhSachMaGDDN.IndexOf(c.ToolTip) > 0)
            //    {
            //        c.Checked = true;
            //        c.ForeColor = System.Drawing.Color.Blue;
            //        c.Font.Bold = true;
            //        //c.Style.Add("color", "#FF3399");
            //        //c.Style.Add("font-weight","bold");
            //    }
            //}
        }
        catch (Exception b)
        {
            string err = b.ToString();
        }
    }
}
